<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class ConfigRepository {

    const configRedisKey = "DB_CONFIG_DATA";

    /**
     * 批量删除配置数据
     * @param $idArr
     * @return bool
     * @throws BasicException
     */
    public function batchDelConfig($idArr) {
        $delConfig = app('model_config')->whereIn('id', $idArr)->delete();

        if ($delConfig) {
            Redis::del(self::configRedisKey);
            return $delConfig;
        }
        throw new BasicException(93044);
    }

    /**
     * 删除配置数据
     * @param $config_name
     * @return bool
     * @throws BasicException
     */
    public function delConfig($config_name) {
        $delConfig = app('model_config')->where(['key'=>$config_name])->delete();

        if ($delConfig) {
            Redis::del(self::configRedisKey);
            return $delConfig;
        }

        if ($delConfig) return $delConfig;
        throw new BasicException(93044);
    }

    /**
     * 查询配置数据
     * @param $config_name
     * @return bool
     */
    public function queryConfig($config_name) {
        $queryConfig = app('model_config')->where(['key'=>$config_name])->first();

        return $queryConfig;
    }

    /**
     * 查询配置数据
     * @param $config_name
     * @return bool
     */
    public function queryByGroup($group) {
        $queryConfig = app('model_config')->where(['group'=>$group])->get();

        return $queryConfig;
    }

    /**
     * 管理员后台查询配置数据
     */
    public function getQueryList(Array $params) {
        $whereArr = [];
        if (!empty($params["group_type"])) {
            $whereArr["group"] = $params["group_type"];
        }

        $whereFunc = function ($query) use ($params) {
            if (!empty($params["keyword"])) {
                $query->where('key', 'like', "%" . $params['keyword'] . "%")
                    ->orWhere('name', 'like', "%" . $params['keyword'] . "%");
            }
        };

        $page = empty($params['page']) ? 1 : $params['page'];
        $perPage = empty($params['perPage']) ? 10 : $params['perPage'];

        $result = app('model_config')->where($whereArr)->where($whereFunc)->orderBy('id','desc')->paginate($perPage, ['*'], '', $page);

        return $result;
    }


    /**
     * 新增配置数据
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function addConfig($param) {
        $addConfig = app('model_config')->create($param);
        Redis::del(self::configRedisKey);
        if ($addConfig) return $addConfig;
        throw new BasicException(93045);
    }

    /**
     * 修改配置数据
     * @param $where
     * @param $param
     * @return bool
     * @throws BasicException
     */
    public function saveConfig($where,$param) {
        $saveConfig = app('model_config')->where($where)->update($param);
        Redis::del(self::configRedisKey);
        if ($saveConfig!==false) return true;
        throw new BasicException(93047);
    }

    public function saveGlobalConfig($config = []) {

        DB::transaction(function () use($config) {
            if($config && is_array($config)){
                foreach ($config as $name => $value) {
                    app('model_config')->where(["key" => $name])->update([
                        "value" => $value
                    ]);
                }
            }
        }, 5);

        Redis::del(self::configRedisKey);
    }

}
